#include "StdAfx.h"
#include "VSqlDataView.h"

// VSqlDataView ///////////////////////////////////////////////////////////////

//
VSqlDataView::VSqlDataView(QWidget *parent)
	:QFrame(parent)
	,m_view(NULL)
	,m_model(NULL)
	,m_proxyModel(NULL)
{
	//setFrameRect(QRect(100,0,0,0));
	///*layuot()->*/setContentsMargins (0,0,0,0);
	
	m_proxyModel = new QSortFilterProxyModel(this);
		//m_treeProxyModel->setSourceModel(m_model);
		//m_treeProxyModel->setFilterRole(Qt::DisplayRole);
		//m_treeProxyModel->setFilterKeyColumn(0);
		//m_treeProxyModel->setFilterFixedString("1");
		
	m_view = new VTableView(this);
		m_view->setModel(m_proxyModel);
		//m_view->setSelectionBehavior(QAbstractItemView::SelectRows);
		//m_view->resizeRowsToContents();

	QVBoxLayout *mainLayout = new QVBoxLayout(this);
		mainLayout->setContentsMargins(0,0,0,0);
		mainLayout->addWidget(m_view);
	setLayout(mainLayout);

	connect(
		m_view->selectionModel(),SIGNAL(
			currentChanged(const QModelIndex &,const QModelIndex &)
		),
		this,SLOT(
			currentChanged(const QModelIndex &,const QModelIndex &)
		)
	);
}

//
VSqlDataView::~VSqlDataView()
{
}

//
void VSqlDataView::setModel(VSqlDataModel *model)
{
	m_model = model;
	m_proxyModel->setSourceModel(m_model);
}

//
bool VSqlDataView::insertDirtyRow()
{
	QModelIndex currentIndex = m_view->currentIndex();

	if (currentIndex.row() + 1 < m_proxyModel->rowCount())
		return false;

	QModelIndex modelIndex = m_proxyModel->mapToSource(currentIndex);
	if (m_model->isDirty(modelIndex))
		return false;

	m_model->insertDirtyRecord();

	m_view->selectionModel()->setCurrentIndex(
		m_proxyModel->index(currentIndex.row() + 1,currentIndex.column()),
		QItemSelectionModel::ClearAndSelect
	);
	return true;
}

/*********************
//
bool VSqlDataView::removeDirtyRow()
{
	QModelIndex currentIndex = m_view->currentIndex();

	if (currentIndex.row() + 1 < m_proxyModel->rowCount())
		return false;

	QModelIndex modelIndex = m_proxyModel->mapToSource(currentIndex);
	if (!m_model->isDirty(modelIndex) || m_proxyModel->rowCount() <= 1)
		return false;

	m_view->selectionModel()->setCurrentIndex(
		m_proxyModel->index(currentIndex.row() - 1,currentIndex.column()),
		QItemSelectionModel::ClearAndSelect
	);

	m_model->removeDirtyRecord();
	return true;
}
**************************/

//
void VSqlDataView::removeSelectedRows()
{
	VIntegerList rowList;
	foreach (const QModelIndex selIndex,m_view->selectionModel()->selectedIndexes())
		rowList.append(m_proxyModel->mapToSource(selIndex).row());

	m_model->removeRows(rowList,m_view);
}

//
void VSqlDataView::currentChanged(const QModelIndex &current,const QModelIndex &previous)
{
	if (!m_model || !current.isValid())
	{
		emit currentChanged(VSqlDataColumn(),QVariant());
		return;
	}
	
	if (current.row() != previous.row())
	{
		QModelIndex modelIndex = m_proxyModel->mapToSource(previous);
		if (m_model->isDirty(modelIndex) && m_proxyModel->rowCount() > 1)
			m_model->removeDirtyRecord();
	}

	QModelIndex metaIndex = m_proxyModel->mapToSource(current);
	VSqlDataColumn dataColumn = m_model->sqlData()->columnList()[metaIndex.column()];
	QVariantList *dataRecord = m_model->recordFromIndex(metaIndex);
	QVariant value = (*dataRecord)[metaIndex.column()];
	
	emit currentChanged(dataColumn,value);
}

// VTableView /////////////////////////////////////////////////////////////////

//
void VTableView::keyPressEvent(QKeyEvent *event)
{
	if (event->modifiers() == Qt::KeyboardModifiers(Qt::ControlModifier))
	{
		if (event->key() == Qt::Key_Delete)
			m_parent->removeSelectedRows();
	}
	else if (event->modifiers() == Qt::KeyboardModifiers())
	{
		if (event->key() == Qt::Key_Down)
		{
			if (m_parent->insertDirtyRow())
				return;
		}
	}

	QTableView::keyPressEvent(event);
}
